package cn.usian.controller;

import cn.usian.common.ResultEnum;
import cn.usian.pojo.Lessee;
import cn.usian.common.Results;
import cn.usian.service.LesseeService;
import io.lettuce.core.ScriptOutputType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("api/lessee")
public class LesseeController {
    @Autowired
    private LesseeService lesseeService;

    @RequestMapping("findPage/{pageNum}/{pageSize}")
    public Results findPage(
            @PathVariable Integer pageNum,
            @PathVariable Integer pageSize,
            @RequestBody Lessee lessee
    ){


        return lesseeService.findPage(pageNum,pageSize,lessee);
    }

    @RequestMapping("findLessee")
    public Lessee findLessee(Long id){
        return lesseeService.findById(id);
    }

    @RequestMapping("addOrUpdate")
    public Results addOrUpdate(@RequestBody Lessee lessee){
        return lesseeService.addOrUpdate(lessee);
    }

    @DeleteMapping("deleteById/{id}")
    public Results deleteById(@PathVariable Long id){
        lesseeService.removeById(id);
        return Results.build(null, ResultEnum.SUCCESS);

    }

    @DeleteMapping("deleteIds")
    public Results deleteIds(String ids){
//        Integer[] array = Arrays.stream(ids.split(",")).map(Integer::parseInt).toArray(Integer[]::new);
        List<Integer> collect = Arrays.stream(ids.split(",")).map(Integer::parseInt).collect(Collectors.toList());
        this.lesseeService.removeByIds(collect);
        return Results.build(null,ResultEnum.SUCCESS);
    }
}
